package com.mcptt.config.group.sub.sub;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Element;
import org.jdom.JDOMException;

import com.cloudptt.api.product.log.Log;

public class Rule {
	
	public static final String Name = "rule";
	public static final String Namespace_Cp = "cp";
	private static final String Namespace_Mcpttgi = "mcpttgi";
	private static final String Name_Id = "id";
	private static final String Name_Actions = "actions";
	private static final String Name_Conditions = "conditions";
	
	private String id = null;
	private Map<String,String> ruleMap = null;
	private boolean relayService = false;
	
	
	public void parse(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		id = elem.getAttributeValue(Name_Id);
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);
			
			if(name == null
					|| name.length() <= 0)
			{
				return;
			}
			if(name.equals(Name_Actions))
			{
				parseSubElem(subElem);
			}
		}  
	}
	public void parseSubElem(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		
		ruleMap = new HashMap<String,String>();
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			String value = subElem.getText();
			Log.debug(this.getClass(), "name = " + name + " value = " + value);

			if(name != null
					&& name.length() > 0
					&& value != null
					&& value.length() > 0)
			{
				ruleMap.put(name, value);
			}
		}  
	}
	
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<").append(Namespace_Cp).append(":").append(Name).append(" ").append(Name_Id).append("=\"").append(id).append("\">").append("\r\n");
		
		strBuf.append("    ").append("<").append(Namespace_Cp).append(":").append(Name_Conditions).append(">").append("\r\n");
		strBuf.append("        ").append("<is-list-member/>");
		strBuf.append("    ").append("</").append(Namespace_Cp).append(":").append(Name_Conditions).append(">").append("\r\n");
		
		strBuf.append("    ").append("<").append(Namespace_Cp).append(":").append(Name_Actions).append(">").append("\r\n");
		for(Iterator<String> itor = ruleMap.keySet().iterator();itor.hasNext();)
		{
			String name = itor.next();
			String value = ruleMap.get(name);
			strBuf.append("    ").append("<");
			if(!"allow-initiate-conference".equals(name)
					&& !"join-handling".equals(name))
			{
				strBuf.append(Namespace_Mcpttgi).append(":");
			}
			strBuf.append(name).append(">");
			strBuf.append(value);
			strBuf.append("</");
			if(!"allow-initiate-conference".equals(name)
					&& !"join-handling".equals(name))
			{
				strBuf.append(Namespace_Mcpttgi).append(":");
			}
			strBuf.append(name).append(">").append("\r\n");
		}
		strBuf.append("    ").append("</").append(Namespace_Cp).append(":").append(Name_Actions).append(">").append("\r\n");
		
		strBuf.append("</").append(Namespace_Cp).append(":").append(Name).append(">").append("\r\n");

		return strBuf.toString();
	}
}
